package com.zy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.mapper.UserMapper;
import com.zy.pojo.User;
import com.zy.vo.PageAto;
import com.zy.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> getUserList(UserVo userVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
                .like(!StringUtils.isEmpty(userVo.getUserName()),"username",userVo.getUserName())
                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
                .gt(userVo.getStime()!=null,"birthday",userVo.getStime())
                .lt(userVo.getEtime()!=null,"birthday",userVo.getEtime());

        return super.list(queryWrapper);
    }

//    @Override
//    public PageAto getPageUserList(Integer pageNo, Integer pageSize, UserVo userVo) {
//        Page page = new Page();
//        page.setSize(pageSize);
//        page.setCurrent(pageNo);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
//                .like(!StringUtils.isEmpty(userVo.getUserName()),"username",userVo.getUserName())
//                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
//                .gt(userVo.getStime()!=null,"birthday",userVo.getStime())
//                .lt(userVo.getEtime()!=null,"birthday",userVo.getEtime());
////        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
////        queryWrapper.eq(userVo.getUserRole()!=null,"User::getUserRole",userVo.getUserRole());
//        IPage<User> ipage=super.page(page,queryWrapper);
//        return PageAto.getInstance(ipage);
//    }
@Override
public PageAto getPageUserList(Integer pageNo, Integer pageSize, UserVo userVo) {
    Page page = new Page();
    page.setSize(pageSize);
    page.setCurrent(pageNo);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
            .like(!StringUtils.isEmpty(userVo.getUserName()),"username",userVo.getUserName())
            .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
            .gt(userVo.getStime()!=null,"birthday",userVo.getStime())
            .lt(userVo.getEtime()!=null,"birthday",userVo.getEtime());
     List<User> list=userMapper.getUserPageList(page,queryWrapper);
    System.out.println(page.getTotal()+"----------------------");
    return new PageAto(list,page.getTotal());
}

    @Override
    public User getUserByUserCode(String userCode) {
        User user = userMapper.getUserByUserCode(userCode);
        return user;
    }
}
